<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<script type="text/javascript">
    var dataGrid;
    
    dataGrid = $("#files_datagrid_list").datagrid({
        url : '${ctx}/file/datagrid.do',
        pagination : true,
        fit : true,
        border : true,
        idField : 'id',
        pageSize : 100,
        rownumbers : true,
        pageList : [ 100, 200, 300, 500,1000],
        sortName : 'number',
        sortOrder : 'desc',
        singleSelect : true,
        columns : [ [ {
            field : 'id',
            hidden:true
        },{
            field : 'numberpart_1',
            hidden:true
        },{
            field : 'numberpart_2',
            hidden:true
        },{
            field : 'numberpart_3',
            hidden:true
        },{
            field : 'isindefile',
            hidden:true
        },{
            field : 'number',
            title : '文号',
            width :  200,
        },{
            field : 'name',
            title : '题名',
            width :  300,
        },{
            field : 'pagenumber',
            title : '页数',
        },{
            field : 'copies',
            title : '份数',
        },{
            field : 'levels',
            title : '密级',
        },{
            field : 'reader',
            title : '审阅人',
        },{
            field : 'isindefile_str',
            title : '是否在档',
        }] ],
        toolbar : [ {
            text : '添加',
            iconCls : 'icon-add',
            handler : function() {
                addFun();
            }
        }, '-', {
            text : '编辑',
            iconCls : 'icon-edit',
            handler : function() {
                edit();
            }
        }, '-', {
            text : '删除',
            iconCls : 'icon-remove',
            handler : function() {
                remove();
            }
        }, '-', {
            text : '条件筛选',
            iconCls : 'icon-search',
            handler : function() {
                searchFun();
            }
        } , '-', {
            text : '清空条件',
            iconCls : 'icon-cancel',
            handler : function() {
                cleanFun();
            }
        }, '-', {
            text : '借出',
            iconCls : 'icon-edit',
            handler : function() {
                outfile();
            }
        }, '-', {
            text : '归还',
            iconCls : 'icon-edit',
            handler : function() {
                infile();
            }
        }, '-', {
            text : '导出表格',
            iconCls : 'icon-edit',
            handler : function() {
                exportexcel();
            }
        }
        ]
    });


    function searchFun() {
        $("#files_datagrid_list").datagrid('load',
            $.serializeObject($('#searchForm'))
        );
    }

    function cleanFun() {
        $("#searchForm input").val('');
        $("#files_datagrid_list").datagrid('load', {});
    }
    function exportexcel() {
        searchFun();
        var data = $.serializeObject($('#searchForm'));
        var url = "";
        url = '${ctx}/file/datagridExcel.do?name='+data.name+'&number='+data.number+'&reader='+data.reader+'&isindefile='+data.isindefile;
        self.location.href=url;

    }
    function addFun() {
        parent.$.modalDialog({
            title : '添加内容',
            width : 820,
            height : 610,
            href : '${ctx}/file/addTo.do',
            resizable : true,
            fit:false,
            buttons : [ {
                text : '保存',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#fileadd_Form');
                    f.submit();
                }
            }]
        });
    }

    function edit(){
        var datagrid = $("#files_datagrid_list");
        var rows = datagrid.datagrid('getChecked');
        console.log(datagrid);
        console.log(rows.length);
        var ids = "";
        if(rows.length == 0){
            $.messager.show({
                title:'提示',
                msg:'请您选择一条数据来做修改操作！'
            });
        }
        if(rows.length > 1){
            $.messager.show({
                title:'提示',
                msg:'只能选择一条数据来做修改操作！'
            });
        }
        if(rows.length == 1){
            id = rows[0].id;
            var dynamic_dialog  = $("<div/>").dialog({
                title: '档案内容修改',
                closed: false,
                cache: false,
                fit:false,
                width : 820,
                height : 610,
                resizable : true,
                href: '${ctx}/file/editTo.do?id=' + id,
                modal: true,
                buttons:[{
                    text : '保存',
                    iconCls: 'icon-add',
                    handler : function() {
                        $('#contentEdit_edit_form').form('submit',{
                            url:'${ctx}/file/edit.do',
                            onSubmit:function(){
                                $.serializeObject($('#contentEdit_edit_form'));
                            },
                            success:function(data){
                                var obj = $.parseJSON(data);
                                if(obj.success){
                                    dynamic_dialog.dialog('close');
                                    datagrid.datagrid('load')
                                }
                                $.messager.show({
                                    title:'提示',
                                    msg:obj.msg
                                });
                            }
                        });
                    }
                }],
                onClose:function(){
                    $(this).dialog('destroy');
                },
                onLoad:function(){
                    $("#contentEdit_edit_form").form('load',rows[0]);
                }

            });
        }
    }
    function outfile(){
        var datagrid = $("#files_datagrid_list");
        var rows = datagrid.datagrid('getChecked');
        var ids = "";
        if(rows.length == 0){
            $.messager.show({
                title:'提示',
                msg:'请您选择一条数据来做借出操作！'
            });
        }
        if(rows.length > 1){
            $.messager.show({
                title:'提示',
                msg:'只能选择一条数据来做借出操作！'
            });
        }
        if(rows.length == 1){
            id = rows[0].id;
            var dynamic_dialog  = $("<div/>").dialog({
                title: '档案借出',
                closed: false,
                cache: false,
                fit:false,
                width : 820,
                height : 610,
                resizable : true,
                href: '${ctx}/file/fileout.do?id=' + id,
                modal: true,
                buttons:[{
                    text : '借出',
                    iconCls: 'icon-add',
                    handler : function() {
                        $('#contentEdit_out_form').form('submit',{
                            url:'${ctx}/file/jie.do',
                            onSubmit:function(){
                                $.serializeObject($('#contentEdit_out_form'));
                            },
                            success:function(data){
                                var obj = $.parseJSON(data);
                                if(obj.success){
                                    dynamic_dialog.dialog('close');
                                    datagrid.datagrid('load')
                                }
                                $.messager.show({
                                    title:'提示',
                                    msg:obj.msg
                                });
                            }
                        });
                    }
                }],
                onClose:function(){
                    $(this).dialog('destroy');
                },
                onLoad:function(){
                    $("#contentEdit_out_form").form('load',rows[0]);
                }

            });
        }
    }
    function infile() {
        var datagrid = $("#files_datagrid_list");
        var rows = datagrid.datagrid('getChecked');
        var ids = "";
        if(rows.length == 0){
            $.messager.show({
                title:'提示',
                msg:'请您选择一条数据来做归还操作！'
            });
        }
        if(rows.length > 1){
            $.messager.show({
                title:'提示',
                msg:'只能选择一条数据来做归还操作！'
            });
        }
        if(rows.length == 1){
            id = rows[0].id;
            var dynamic_dialog  = $("<div/>").dialog({
                title: '档案归还',
                closed: false,
                cache: false,
                fit:false,
                width : 820,
                height : 610,
                resizable : true,
                href: '${ctx}/file/filein.do?id=' + id,
                modal: true,
                buttons:[{
                    text : '归还',
                    iconCls: 'icon-add',
                    handler : function() {
                        $('#contentEdit_in_form').form('submit',{
                            url:'${ctx}/file/huan.do',
                            onSubmit:function(){
                                $.serializeObject($('#contentEdit_in_form'));
                            },
                            success:function(data){
                                var obj = $.parseJSON(data);
                                if(obj.success){
                                    dynamic_dialog.dialog('close');
                                    datagrid.datagrid('load')
                                }
                                $.messager.show({
                                    title:'提示',
                                    msg:obj.msg
                                });
                            }
                        });
                    }
                }],
                onClose:function(){
                    $(this).dialog('destroy');
                },
                onLoad:function(){
                    $("#contentEdit_in_form").form('load',rows[0]);
                }

            });
        }
    }
    function upset() {
        var datagrid = $("#files_datagrid_list");
        var rows = datagrid.datagrid('getChecked');
        if(rows.length > 0){
            $.messager.confirm('确认','确认要置顶吗？',function(r){
                if (r){
                    $.ajax({
                        url:'messageshow/upset.do',
                        data:{
                            id:rows[0].id
                        },
                        dataType:'json',
                        success:function(data){
                            datagrid.datagrid('load');
                            datagrid.datagrid('unselectAll');//使全部的行形成未選中狀態
                            $.messager.show({
                                title:'提示',
                                msg:data.msg
                            });
                        }
                    });
                }
            });
        }else{
            $.messager.show({
                title:'提示',
                msg:'请至少选择一行来做操作！'
            });
        }
    }

    function remove() {
        var datagrid = $("#files_datagrid_list");
        var rows = datagrid.datagrid('getChecked');
        var ids = [];
        console.log(rows);
        if(rows.length > 0){
            $.messager.defaults.ok = "确定"
            $.messager.defaults.cancel = "取消"
            $.messager.confirm('确认','确认要要删除吗？',function(r){
                if (r){
                    console.log(rows);
                    for(var i=0;i<rows.length;i++){
                        ids.push(rows[i].id);
                    }
                    $.ajax({
                        url:'${ctx}/file/del.do',
                        data:{
                            ids:ids.join(',')
                        },
                        dataType:'json',
                        success:function(data){
                            datagrid.datagrid('load');
                            datagrid.datagrid('unselectAll');//使全部的行形成未選中狀態
                            $.messager.show({
                                title:'提示',
                                msg:data.msg
                            });
                        }
                    });
                }
            });
        }else{
            $.messager.show({
                title:'提示',
                msg:'请至少选择一行来做删除操作！'
            });
        }
    }
</script>

<div class="easyui-layout" data-options="fit : true,border : false">
    <div data-options="region:'north',title:'条件筛选区',border:false" style="height:70px;overflow:hidden;">
        <form id="searchForm" method="post">
            <table>
                <tr>
                    <th>文号</th>
                    <td><input class="easyui-textbox" data-options="prompt:'可模糊查询'" name="number"  style="width:400px;height:30px;"/></td>
                    <th>题名</th>
                    <td><input class="easyui-textbox" data-options="prompt:'可模糊查询'" name="name"  style="width:400px;height:30px;"/></td>
                    <th>审阅人</th>
                    <td><input class="easyui-textbox" data-options="prompt:'可模糊查询'" name="reader"  style="width:400px;height:30px;"/></td>
                    <th>是否在档</th>
                    <td><input id="isindefile" name="isindefile" class="easyui-combobox" data-options="editable:false,
                        valueField: 'value',
                        textField: 'text',
                        data: [
                        {text: '在档',value: '1'},
                        {text: '不在档',value: '0'},
                        ]" style="height:30px;width:230px;"/>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <div data-options="region:'center',border:false">
        <table id="files_datagrid_list"></table>
    </div>
</div>


